package com.cwz.excel;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.cwz.mapper.MaterialMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wenZhen
 * @create 2024-08-17 16:53
 */

@Service
public class MaterialExcelListener implements ReadListener<MaterialExcel> {

    @Resource
    private MaterialMapper materialMapper;


    static final ThreadLocal<List<MaterialExcel>> THREAD_LOCAL=new ThreadLocal<>();

    @Override
    public void invoke(MaterialExcel materialExcel, AnalysisContext analysisContext) {
        List<MaterialExcel> list;
        if ((list=THREAD_LOCAL.get())==null){
            list = new ArrayList<>();
        }

        list.add(materialExcel);

        THREAD_LOCAL.set(list);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        List<MaterialExcel> list = THREAD_LOCAL.get();
        materialMapper.insertBatch(list);

    }
}
